package com.alibaba.algo.controller.user;

import com.alibaba.algo.apiimpl.user.SysEmployeeServiceImpl;
import com.alibaba.algo.cooperation.common.PagerEntity;
import com.alibaba.algo.cooperation.usercomponent.vo.req.user.SelectEmployeeListRequestVO;
import com.alibaba.algo.cooperation.usercomponent.vo.res.user.EmployeeOrgResVO;
import com.alibaba.algo.cooperation.usercomponent.vo.res.user.EmployeePositionResVO;
import com.alibaba.algo.cooperation.usercomponent.vo.res.user.SelectEmployeeBaseAndOrgInfoResVO;
import com.aliyun.gsuite.api.IClientForUser;
import com.aliyun.gsuite.model.CapRoleInfo;
import com.aliyun.gsuite.model.CapUserInfo;
import com.aliyun.gsuite.model.user.BaseUserInfo;
import com.aliyun.gsuite.model.user.OrgInfo;
import com.aliyun.gsuite.model.user.PositionInfo;
import com.aliyun.gsuite.response.BaseResult;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class SysEmployeeAdapterController implements IClientForUser {

    @Autowired
    private SysEmployeeServiceImpl sysEmployeeService;

    @Override
    //最小化的云巧实现类中不需要实现此方法
    public BaseResult<Page<CapRoleInfo>> listRolesByUserId(String appId, String userId) {
        return null;
    }

    @Override
    //获取用户的详细信息
    public BaseResult<CapUserInfo> listUserDetail(String userId) {
        SelectEmployeeListRequestVO requestVO = new SelectEmployeeListRequestVO();
        requestVO.setMobilePhone(userId);
        requestVO.setPageIndex(1);
        requestVO.setPageSize(10);
        PagerEntity entity = sysEmployeeService.selectPage(requestVO);
        List<SelectEmployeeBaseAndOrgInfoResVO> lists = entity.getDataList();
        SelectEmployeeBaseAndOrgInfoResVO employeeBaseAndOrgInfoResVO = lists.size() >= 0 ? lists.get(0) : null;
        CapUserInfo user = null;
        if (employeeBaseAndOrgInfoResVO != null) {
            user = new CapUserInfo();
            BaseUserInfo baseUserInfo = new BaseUserInfo();
            baseUserInfo.setId(employeeBaseAndOrgInfoResVO.getId());
            baseUserInfo.setUserId(employeeBaseAndOrgInfoResVO.getMobilePhone());
            baseUserInfo.setUserName(employeeBaseAndOrgInfoResVO.getEmployeeName());
            user.setBaseInfo(baseUserInfo);

            List<OrgInfo> orgInfoList = new ArrayList<>();
            for (EmployeeOrgResVO employeeOrgResVO : employeeBaseAndOrgInfoResVO.getOrgList()) {
                OrgInfo orgInfo = new OrgInfo();
                orgInfo.setOrgId(employeeOrgResVO.getUuid());
                orgInfo.setOrgCode(String.valueOf(employeeOrgResVO.getEmployeeOrgId()));
                orgInfo.setOrgName(employeeOrgResVO.getOrgName());
                orgInfoList.add(orgInfo);
            }
            user.setOrgInfoList(orgInfoList);

            List<PositionInfo> positionInfoList = new ArrayList<>();
            for (EmployeePositionResVO employeePositionResVO : employeeBaseAndOrgInfoResVO.getPositionList()) {
                PositionInfo positionInfo = new PositionInfo();
                positionInfo.setPositionCode(String.valueOf(employeePositionResVO.getEmployeePositionId()));
                positionInfo.setPositionName(employeePositionResVO.getPositionName());
                positionInfoList.add(positionInfo);
            }
            user.setPositionInfoList(positionInfoList);

        }
        BaseResult<CapUserInfo> result = new BaseResult();
        result.setData(user);
        return result;
    }
}
